package com.fingard.dsp.bank.directbank.cncbi01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsHisBalDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class CNCBI01Balance extends CNCBI01Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        //余额查询返回ATS的对象
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            //赋值通用参数
            CNCBI01Header bankHeader = new CNCBI01Header();
            bankHeader.action = "DLBALQRY";

            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader(xmlWriter, bankHeader);

            xmlWriter.writeStartElementByAttr("list", "<list name=\"userDataList\">");
            while (p_req7506.readRecord()) {
                xmlWriter.writeStartElement("row");
                xmlWriter.writeElementString("accountNo", p_req7506.readedDetail.actNum);
                xmlWriter.writeEndElement();
            }

            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();

                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);

                if (tmpBkReader != null) {
                    String tmpStatus = tmpBkReader.getFirstText("/stream/status");
                    String tmpStatusText = tmpBkReader.getFirstText("/stream/statusText");
                    if (tmpStatus != null && tmpStatus.equals("AAAAAAA")) {
                        while (tmpBkReader.read("/stream/list/row")) {
                            RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                            if (tmpBkReader.readedXElement.getSingleText("status").equals("AAAAAAA")) {
                                tmpRetDetail.respCode = "0";
                                tmpRetDetail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleText("currencyID"));
                                tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("accountNo");
                                tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("usableBalance");
                                tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleText("balance");
                                tmpRetDetail.frozenBal = tmpBkReader.readedXElement.getSingleText("forzenAmt");
                                //currencyID币种
                                String tmpLastTranDate = tmpBkReader.readedXElement.getSingleText("lastTranDate");
                                if (!StringHelper.isNullOrEmpty(tmpLastTranDate) && tmpLastTranDate.length() == 8) {
                                    tmpRetDetail.lastTranDate = tmpLastTranDate.substring(0, 4) + "-" + tmpLastTranDate.substring(4, 6) + "-" + tmpLastTranDate.substring(6, 8);
                                }
                            } else {
                                tmpRetDetail.respCode = "-1";
                                tmpRetDetail.respInfo = tmpBkReader.readedXElement.getSingleText("status") + "-" + tmpBkReader.readedXElement.getSingleText("statusText");
                            }
                            retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);

                        }
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpStatusText);
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpSbMsg.toString());
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(tmpStrRet[1]);
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        RetAts7505HisBalQry retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
        String tmpRespCode = "";
        String tmpRespInfo = "";
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            CNCBI01Header bankHeader = new CNCBI01Header();
            bankHeader.action = "DLHBLQRY";
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeElementString("accountNo", p_req7505.actNum);
            xmlWriter.writeElementString("startDate", p_req7505.dateFrom);
            xmlWriter.writeElementString("endDate", p_req7505.dateTo);
            xmlWriter.writeEndDocument();

            String[] tmpRetBkStr = sendToBank(xmlWriter.toXmlString());

            XmlTextReader tmpBkReader;
            if (tmpRetBkStr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                tmpBkReader = loadBankRespXmlReader(tmpRetBkStr[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpStatus = tmpBkReader.getFirstText("/stream/status");
                    String tmpStatusText = tmpBkReader.getFirstText("/stream/statusText");
                    if (isActiveRetCode(tmpStatus)) {
                        while (tmpBkReader.read("/stream/list/row")) {
                            RetAtsHisBalDetail tmpRetDetail = new RetAtsHisBalDetail();
                            String tmpETYDAT = tmpBkReader.readedXElement.getSingleText("date");
                            if (tmpETYDAT.length() == 8) {
                                tmpRetDetail.balDate = tmpETYDAT.substring(0, 4) + "-" + tmpETYDAT.substring(4, 6) + "-" + tmpETYDAT.substring(6, 8);
                            } else {
                                tmpRetDetail.balDate = tmpETYDAT;
                            }
                            tmpRetDetail.actBal = tmpBkReader.readedXElement.getSingleText("balance");
                            retAts.writeRetDetail(tmpRetDetail);
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpStatusText;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpRetBkStr[1];
            }

        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if (tmpRespCode.equals("-1")) {
            retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("返回结果为空，所查询时间段内不存在历史余额");
        }
        retAts.writeEnd();
        return retAts;
    }
}
